package wrapper;

import java.awt.Color;
import java.awt.Graphics;
import java.util.List;

import javax.swing.JPanel;

import dijkstra.Edge;
import dijkstra.Vertex;

public class JShowVertex extends JPanel {
	private static final long serialVersionUID = 1L;
	private Vertex[] lista;
	private Vertex[] coda;
	public JShowVertex(int dimx,int dimy){
		super();
		setSize((dimy+1)*16,(dimx+1)*16);
		
		setBackground(Color.BLACK);
	}
	public void updateVertex(Vertex[] lista,List<Vertex> coda){
		this.lista=lista;
		this.coda=new Vertex[0];
		this.coda=coda.toArray(this.coda);
		
		repaint();
		validate();
	}
    public void paint(Graphics g)
    {
    	super.paint(g);
    	int height=16;
    	int width=16;
    	if(lista!=null){
    		for(int i=0;i<lista.length;i++){
    			if(lista[i].tipo==Vertex.EDGE){
    				g.setColor(Color.GRAY);
    				g.fillRect(lista[i].y*16+2, lista[i].x*16+2, height-4, width-4);
    			}
    			else if(lista[i].tipo==Vertex.PACMAN){
    				g.setColor(Color.YELLOW);
    				g.fillOval(lista[i].y*16+2, lista[i].x*16+2, height-4, width-4);
    			}
    			else{
    				g.setColor(Color.magenta);
    				g.fillOval(lista[i].y*16+2, lista[i].x*16+2, height-4, width-4);
    			}
    			for(Edge ed:lista[i].adjacencies){
    				mostra(lista[i],ed,g);
    			}
    		}
    	}
    	if(coda!=null){
    		for(int i=0;i<coda.length-1;i++){
    			disegnaLinea(coda[i],coda[i+1],g);
    		}
    	}
    	

    }
    private final int SPESSORE=2;
    private void disegnaLinea(Vertex start, Vertex end, Graphics g) {
    	g.setColor(Color.CYAN);
    	
    	if(start.x==end.x){

    		
    		//destra o sinistra
    		for(int i=0;i<SPESSORE;i++){
    			g.drawLine((start.y)*16+8, start.x*16+8+i, end.y*16+8, end.x*16+8+i);
    			g.drawLine((start.y)*16+8, start.x*16+8-i, end.y*16+8, end.x*16+8-i);
    		}
    		
    		
    		

    	}
    	else{
    		for(int i=0;i<SPESSORE;i++){
    			g.drawLine((start.y)*16+8+i, start.x*16+8, end.y*16+8, end.x*16+8+i);
    			g.drawLine((start.y)*16+8-i, start.x*16+8, end.y*16+8, end.x*16+8-i);
    		}
    		


    	}
		
	}
	private void mostra(Vertex start, Edge ed, Graphics g) {
		//int disturb=(int)(Math.random()*3);
		int disturb=0;
		Vertex end=ed.target;
		int costo=(int)ed.weight;
		costo=Math.min(costo, 255);
		int costoBlu=Math.min(Math.max((int)ed.weight-255,0),255);
		g.setColor(new Color(costoBlu,(255-costo),costo-costoBlu));
		if(start.x==end.x){
			
			if(start.y<end.y){
				//destra
				g.drawLine((start.y+1)*16, start.x*16+disturb, end.y*16, end.x*16+disturb);
				disegnaFreccia(end.y*16, end.x*16+disturb,-3,0,g);
			}
			else{
				//sinistra
				g.drawLine(start.y*16, (start.x+1)*16-disturb, (end.y+1)*16, (end.x+1)*16-disturb);
				disegnaFreccia((end.y+1)*16, (end.x+1)*16-disturb,+3,0,g);
			}
		}
		else{
			if(start.x<end.x){
				//giu
				g.drawLine((start.y+1)*16-disturb, (start.x+1)*16, (end.y+1)*16-disturb, end.x*16);
				disegnaFreccia( (end.y+1)*16-disturb, end.x*16,0,-3,g);
			}
			else{
				//su
				g.drawLine(start.y*16+disturb, start.x*16, end.y*16+disturb, (end.x+1)*16);
				disegnaFreccia(end.y*16+disturb, (end.x+1)*16,0,3,g);
			}
			
		}
	}
	private void disegnaFreccia(int x, int y, int dx, int dy, Graphics g) {
		if(dy==0){
			g.drawLine(x, y, x+dx, y-2);
			g.drawLine(x, y, x+dx, y+2);
		}
		else{
			g.drawLine(x, y, x-2, y+dy);
			g.drawLine(x, y, x+2, y+dy);
		}
		
		
	}
}
